﻿namespace Yaaf.WP7.NativeAccess.IO.Internals.HTC.COM_NativeAccess
{
    using System;
    using System.IO;

    using Yaaf.WP7.NativeAccess.ComWrapper.HTC.COM_NativeAccess;

    internal class File
    {
        private IntPtr handle;
        private global::Yaaf.WP7.NativeAccess.IO.Internals.HTC.COM_NativeAccess.FileAccess fileAccess;
        private long length;

        internal File()
        {
        }

        internal void Close()
        {
            FileSystem.CloseFile(this.handle);
        }

        internal long Seek(long offset, SeekOrigin origin)
        {
            global::Yaaf.WP7.NativeAccess.IO.Internals.HTC.COM_NativeAccess.SeekOriginInternal a;
            switch (origin)
            {
                case SeekOrigin.Begin:
                    a = global::Yaaf.WP7.NativeAccess.IO.Internals.HTC.COM_NativeAccess.SeekOriginInternal.Begin;
                    break;

                case SeekOrigin.Current:
                    a = global::Yaaf.WP7.NativeAccess.IO.Internals.HTC.COM_NativeAccess.SeekOriginInternal.Current;
                    break;

                case SeekOrigin.End:
                    a = global::Yaaf.WP7.NativeAccess.IO.Internals.HTC.COM_NativeAccess.SeekOriginInternal.End;
                    break;

                default:
                    throw new ArgumentOutOfRangeException("origin");
            }
            return (long) FileSystem.Seek(this.Handle, (long) ((int) offset), a);
        }

        internal int ReadFile(byte[] buffer, int offset, int numberofBytesToRead)
        {
            int lpNumberOfBytesRead = 0;
            FileSystem.ReadFile(this.Handle, buffer, numberofBytesToRead, offset, out lpNumberOfBytesRead);
            return lpNumberOfBytesRead;
        }

        public static FileStream Open(string lpFilename, global::Yaaf.WP7.NativeAccess.IO.Internals.HTC.COM_NativeAccess.FileAccess dwDesiredAccess, FileShare dwShareMode, CreationDisposition dwCreationDisposition)
        {
            FileAttributes normal = FileAttributes.Normal;
            File file = new File();
            file.FileAccess = dwDesiredAccess;
            FileSystem.OpenFile(lpFilename, dwDesiredAccess, dwShareMode, dwCreationDisposition, normal, out file.handle);
            if (file.Handle == IntPtr.Zero)
            {
                throw new Exception("Could not open file");
            }
            file.length = FileSystem.GetFileSize(file.Handle);
            return new FileStream(file);
        }

        internal global::Yaaf.WP7.NativeAccess.IO.Internals.HTC.COM_NativeAccess.FileAccess FileAccess
        {
            get
            {
                return this.fileAccess;
            }
            private set
            {
                this.fileAccess = value;
            }
        }

        internal IntPtr Handle
        {
            get
            {
                return this.handle;
            }
            private set
            {
                this.handle = value;
            }
        }

        internal long Length
        {
            get
            {
                return this.length;
            }
            private set
            {
                this.length = value;
            }
        }

        internal long Position
        {
            get
            {
                return (long) FileSystem.Seek(this.Handle, 0L, global::Yaaf.WP7.NativeAccess.IO.Internals.HTC.COM_NativeAccess.SeekOriginInternal.Current);
            }
        }
    }
}

